define(['./../initial', './../service/mapInstance', './../../prjStatus/index', './../service/toggleSideBar'], function() {
    var app = angular.module('App.home');
    app.directive('prjSummaryPopup', ['mapInstance', '$compile', '$rootScope', 'prjStatusChartModalHelper', 'toggleSideBar', '$q',
        function(mapInstance, $compile, $rootScope, prjStatusChartModalHelper, toggleSideBar, $q) {
            return {
                restrict: 'A',
                scope: {
                    'prj': '='
                },
                link: function(scope, el, attrs) {
                    prjStatusChartModalHelper.close();
                    var prj = scope.prj;
                    scope.prjMarkerClick = function() {
                        if (prj.hasJsonLayer === false) {
                            alert('尚未制作布点图!!');
                        } else {
                            prjStatusChartModalHelper.close();
                            var map = mapInstance.getMap();
                            map.fireEvent('prjMarkerClick', {
                                prjId: prj.prjId
                            });
                        }
                    };
                    scope.openStatusChartModal = function() {
                        if (prj.status) {
                            $q.all([toggleSideBar.toggleBottomSideBar('hide'), prj.getStatusList(true)]).then(function() {
                                return prjStatusChartModalHelper.open(prj);
                            });
                        }
                    };
                    scope.openPrjStatusModal = function() {
                        var content = $('#singleChartModalCotent').empty();
                        /*Test for iamges ,remove when production */
                        var prjImgUrls = prj.status.imagesUrl.map(function(item) {
                            return {
                                imgUrl: item,
                                description: ""
                            };
                        });
                        scope.prjStatusInformation = {
                            prjStatus: prj.status.detail ? prj.status.detail : prj.status.title,
                            prjImgUrls: prjImgUrls
                        };
                        var div = document.createElement('div');
                        div.setAttribute('data-prj-Status-information', 'prjStatusInformation');
                        div.setAttribute('data-prj-Status-Modal', '');

                        var element = angular.element(div);
                        content[0].appendChild(div);
                        $compile(element)(scope);

                    };
                },
                templateUrl: 'partials/prjSummaryPopup.html'
            };
        }
    ]);

});
